/**
 * Created by Joker on 2017/4/19.
 */



let formatJs = {
  //时间转化
 FParseToDate:function (value) {
    if (value == null || value == '') {
        return undefined;
    }
    var dt;
    if (value instanceof Date) {
        dt = value;
    }
    else {
        if (!isNaN(value)) {
            dt = new Date(value);
        }
        else if (value.indexOf('/Date') > -1) {
            value = value.replace(/\/Date\((-?\d+)\)\//, '$1');
            dt = new Date();
            dt.setTime(value);
        } else if (value.indexOf('-') > -1) {
            //dt = new Date(Date.parse(value.replace(/-/g, '/')));
            dt = new Date(value.replace(/-/g, '/'));
        } else {
            dt = new Date(value);
        }
    }
    return dt;
},

//格式化时间
// Date.prototype.format = function (format) {
//     var o = {
//         "M+": this.getMonth() + 1,
//         "d+": this.getDate(),
//         "h+": this.getHours(),
//         "m+": this.getMinutes(),
//         "s+": this.getSeconds(),
//         "q+": Math.floor((this.getMonth() + 3) / 3),
//         "S": this.getMilliseconds()
//     };
//     if (/(y+)/.test(format))
//         format = format.replace(RegExp.$1,
//                 (this.getFullYear() + "").substr(4 - RegExp.$1.length));
//     for (var k in o)
//         if (new RegExp("(" + k + ")").test(format))
//             format = format.replace(RegExp.$1,
//                     RegExp.$1.length == 1 ? o[k] :
//                         ("00" + o[k]).substr(("" + o[k]).length));
//     return format;
// };




  // 秒数转日期
  dateFormat: function (str) {
    let oDate = new Date(str),
      oYear = oDate.getFullYear(),
      oMonth = oDate.getMonth() + 1,
      oDay = oDate.getDate(),
      oHour = oDate.getHours(),
      oMin = oDate.getMinutes(),
      oSen = oDate.getSeconds(),
      oTime = oYear + '-' + this.getzf(oMonth) + '-' + this.getzf(oDay) + ' ' + this.getzf(oHour) + ':' + this.getzf(oMin) + ':' + this.getzf(oSen)//最后拼接时间
    return oTime
  },
  //补0操作
  getzf: function (num) {
    if (parseInt(num) < 10) {
      num = '0' + num
    }
    return num
  },
  //number转千分位
  numToMoneyValue: function (s) {
    let n = 2
    if (typeof (s) == 'number') {
      s = s.toFixed(n)
    }
    if (s == '' || s == null) return ''
    n = n > 0 && n <= 20 ? n : 2
    s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''
    if (s * 1 == 0) return ''
    let l = s.split('.')[0].split('').reverse(),
      r = s.split('.')[1]
    let t = ''
    for (let i = 0; i < l.length; i++) {
      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '')
    }
    return t.split('').reverse().join('').replace('-,', '-') + '.' + r
  },
  stringToMoneyValue: function (str) {
    if (str == null || str == "" || str == undefined) return 0;
    if (str.length == 1 && str == "-") return 0;
    try {
      str = str.replace(/,/g, "");
      if (isNaN(str)) return 0;
      return Number(str);
    } catch (e) {
      return 0;
    }
  },

  //加减乘除
  accDiv: function (arg1, arg2) {
    let t1 = 0, t2 = 0, r1, r2
    try {
      t1 = arg1.toString().split('.')[1].length
    } catch (e) {
    }
    try {
      t2 = arg2.toString().split('.')[1].length
    } catch (e) {
    }
    r1 = Number(arg1.toString().replace('.', ''))
    r2 = Number(arg2.toString().replace('.', ''))
    return parseFloat(((r1 / r2) * Math.pow(10, t2 - t1)).toFixed(2))
  },
  accMul: function (arg1, arg2) {
    let m = 0, s1 = arg1.toString(), s2 = arg2.toString()
    try {
      m += s1.split('.')[1].length
    } catch (e) {
    }
    try {
      m += s2.split('.')[1].length
    } catch (e) {
    }
    return parseFloat((Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)).toFixed(2))
  },
  accAdd: function (arg1, arg2) {
    let r1, r2, m
    try {
      r1 = arg1.toString().split('.')[1].length
    } catch (e) {
      r1 = 0
    }
    try {
      r2 = arg2.toString().split('.')[1].length
    } catch (e) {
      r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    return parseFloat(((arg1 * m + arg2 * m) / m).toFixed(2))
  },
  accSub: function (arg1, arg2) {
    let r1, r2, m, n
    try {
      r1 = arg1.toString().split('.')[1].length
    } catch (e) {
      r1 = 0
    }
    try {
      r2 = arg2.toString().split('.')[1].length
    } catch (e) {
      r2 = 0
    }
    m = Math.pow(10, Math.max(r1, r2))
    //last modify by deeka
    //动态控制精度长度
    n = (r1 >= r2) ? r1 : r2
    return parseFloat(((arg2 * m - arg1 * m) / m).toFixed(2))
  },
  // 乘
  mul: function (arg) {
    return this.accMul(arg, this)
  },
  // 除
  div: function (arg) {
    return this.accDiv(this, arg)
  },
  // 减
  sub: function (arg) {
    return this.accSub(arg, this)
  },
  // 加
  add: function (arg) {
    return this.accAdd(arg, this)
  }
}
export default formatJs
